// 将高德地图API获取的原始数据转为element组件使用的格式
export function districtFormatter(raw) {
  const newTree = []
  raw.forEach(node => {
    const newNode = {
      label: node.name,
      value: node.name // node.adcode
    }

    // 如果有下一层
    if (node.districts && node.districts.length > 0) {
      newNode.children = districtFormatter(node.districts)
    }
    newTree.push(newNode)
  })
  return newTree
}

// nodejs 测试用
// const axios = require('axios')
// let rawdata = []
// axios({
//   url: 'https://restapi.amap.com/v3/config/district',
//   params: {
//     key: '2fe342d18d1b6aa0eebbbd04e92d56f8',
//     subdistrict: 2
//   }
// }).then(res => {
//   rawdata = res.data.districts
//   //   console.log(rawdata)
//   const mydata = formatter(rawdata)
//   console.log(mydata)
//   console.log(mydata[0].children)
// })
